//
//  HJNetworkReachabilityManager.m
//  KaiChat
//
//  Created by lx on 2023/11/10.
//

#import "HJNetworkReachabilityManager.h"
  
@interface HJNetworkReachabilityManager ()

@property (nonatomic, strong) NSNumber *status;


@end

@implementation HJNetworkReachabilityManager

+ (instancetype)shareManager {
    static HJNetworkReachabilityManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

- (void)monitorNetworkStatus {
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (self.status && (self.status.intValue != status)) {
            NSLog(@"网络发送改变");
            [[NSNotificationCenter defaultCenter] postNotificationName:networkingReachabilityDidChangeNotification object:[NSNumber numberWithInteger:status]];
        }
        
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"当前网络：未知网络");
                break;
                
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"当前网络：无网络");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"当前网络：蜂窝数据");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"当前网络：无线网络");
                break;
                
            default:
                break;
        }
        self.status = @(status);
    }];
    
    [manager startMonitoring];
}

- (BOOL)reachable {
    return  _status.intValue == AFNetworkReachabilityStatusReachableViaWWAN || _status.intValue == AFNetworkReachabilityStatusReachableViaWiFi;
}
 
- (AFNetworkReachabilityStatus)networkReachabilityStatus {
    return _status.intValue;
}

@end

